package root.electronclass.electronclass.other

import android.content.Context
import android.content.Intent
import android.media.PlaybackParams
import android.os.Build
import android.view.View.INVISIBLE
import android.widget.MediaController
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.LogUtils
import root.electronclass.electronclass.common.base.ViewBindingActivity
import root.electronclass.electronclass.databinding.ActivityVideoBinding
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.io.UnsupportedEncodingException
import java.net.URLEncoder


class VideoActivity : ViewBindingActivity<ActivityVideoBinding>() {

    private var savePosition: Int = 0
    private lateinit var exoPlayer: ExoPlayer

    override fun init() {
        binding.back.back.setOnClickListener {
            finish()
        }

        var url = intent.getStringExtra("Path")
//       var url = "http://api.smart.ztengit.com/api/uploadmodule/public/1816362487138127872/下载 (1).mp4"
        // 对视频URL进行编码
        try {
            url = URLEncoder.encode(url, "UTF-8").replace("+", "%20")
            url = url.replace("%2F", "/") // 保留路径分隔符
            url = url.replace("%3A", ":") // 保留冒号
        } catch (e: UnsupportedEncodingException) {
            e.printStackTrace()
        }
        LogUtils.iTag("videoActivity","$url")
        val mediaItem = MediaItem.fromUri(url.orEmpty())
        exoPlayer = ExoPlayer.Builder(this).build()
        binding.video.player = exoPlayer
        exoPlayer.setMediaItem(mediaItem)
        exoPlayer.prepare()
        exoPlayer.playWhenReady = true
//        binding.video.setVideoPath(url)
//        //设置导航控制布局
//        binding.video.setMediaController(MediaController(this))
//        binding.video.setOnPreparedListener {
//            binding.progressBar2.visibility = INVISIBLE
//            binding.progressBar.max = it.duration
//            it.isLooping = true//循环播放
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                it.playbackParams = PlaybackParams().apply {
//                    speed = 1f //倍数播放
//                    // pitch = 0.7f //变声，提高音调 尖锐，减少低沉
//                }
//            }
//            it.seekTo(savePosition)
//        }
//
//        lifecycleScope.launch {
//            while (true) {
//                if (binding.video.isPlaying) {
//                    binding.progressBar.progress = binding.video.currentPosition
//                }
//                delay(500)
//            }
//        }
//
//        // 启动视频播放
//        binding.video.start()

    }

    override fun onPause() {
        super.onPause()
        exoPlayer.pause() // 暂停播放
//        savePosition = binding.video.currentPosition
    }

    override fun onDestroy() {
        super.onDestroy()
//        binding.video.stopPlayback()
        exoPlayer.release() // 释放资源
    }

    companion object {

        @JvmStatic
        fun startActivity(context: Context, path: String) {
            val intent = Intent(context, VideoActivity::class.java)
            intent.putExtra("Path",path)
            context.startActivity(intent)
        }
    }
}